package com.example.demo.controller;

import com.example.demo.Dao.DaoFactory;
import com.example.demo.bean.User;
import com.example.demo.service.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Controller
@CrossOrigin
public class UserController {
    @Autowired
   // private JdbcTemplate jdbcTemplate;
    UserDao dao = DaoFactory.getUserDao();
    @RequestMapping("/getUsers")
    public String getDbType(Model m){
        List<User> list=dao.findUserList();
        m.addAttribute("user",list);
        return "user";
       /* String sql = "select * from guest";
        List<Map<String, Object>> list =  jdbcTemplate.queryForList(sql);
        for (Map<String, Object> map : list) {
            Set<Map.Entry<String, Object>> entries = map.entrySet();// 通过entrySet()方法将map集合中的映射关系(key-value)取出（这个关系就是Map.Entry类型）
            if (entries != null) {
                Iterator<Map.Entry<String, Object>> iterator = entries.iterator();// 将关系集合entryset进行迭代，存放到迭代器中
                while (iterator.hasNext()) {
                    Map.Entry<String, Object> entry = (Map.Entry<String, Object>) iterator.next();// 获取Map.Entry关系对象
                    Object key = entry.getKey();
                    Object value = entry.getValue();

                  /*  if("name".equals(key)&&"zhangsan".equals(value))
                    {System.out.println(123456789);
                    }

                    System.out.println(key + ":" + value);
                    if("price".equals(key))
                    {System.out.println(); }
                }
            }
        }
        m.addAttribute("user",list);
        return "user";*/
    }

    @RequestMapping("/user/insert")
    public void insert(HttpServletRequest request, HttpServletResponse response , Model m) throws Exception {
        long id_insert = Integer.parseInt(request.getParameter("id"));
        double score_insert=Double.parseDouble(request.getParameter("score"));
        int level_insert=Integer.parseInt(request.getParameter("level"));
        double balance_insert=Double.parseDouble(request.getParameter("balance"));
        User user = new User();
        user.setId(id_insert);
        user.setScore(score_insert);
        user.setLevel(level_insert);
        user.setBalance(balance_insert);
        dao.add(user);
    }

    @RequestMapping("/user/update")
    public void update(HttpServletRequest request, HttpServletResponse response , Model m) throws Exception {
        long id_update = Integer.parseInt(request.getParameter("id"));
        double score_update=Double.parseDouble(request.getParameter("score"));
        int level_update=Integer.parseInt(request.getParameter("level"));
        double balance_update=Double.parseDouble(request.getParameter("balance"));
        User user = new User();
        user.setId(id_update);
        user.setScore(score_update);
        user.setLevel(level_update);
        user.setBalance(balance_update);
        dao.update(user);
    }
}
